package com.qf.flash.mvp.presenter;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.qf.dblib.dao.entity.ChatUserBean;
import com.qf.dblib.dao.entity.UserInfo;
import com.qf.flash.http.HttpManager;
import com.qf.flash.http.param.RegisterParam;
import com.qf.flash.mvp.contract.LoginRegisterContract;
import com.qf.flash.utils.LogUtil;
import com.qf.flashlib.base.BasePresenter;
import com.qf.flashlib.base.BaseSubscriber;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.util.RxUtils;

import java.util.HashMap;

/**
 * 登录注册
 * 2020/3/6 9:35
 */
public class LoginRegisterPresenter extends BasePresenter<LoginRegisterContract.View> implements LoginRegisterContract.Presenter {

    @Override
    public void login(String tel, String password) {
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .LOGIN(tel, password)
                .compose(RxUtils.<UserInfo>handleResult())
                .compose(RxUtils.<UserInfo>io_main())
                .subscribeWith(new BaseSubscriber<UserInfo>(mRootView) {
                    @Override
                    public void onSuccess(UserInfo info) {

                        ChatUserBean chatUserBean = new ChatUserBean();
                        chatUserBean.setUid(info.getUid());
                        chatUserBean.setMobile(info.getMobile());
                        chatUserBean.setPhoto(info.getPhoto());
                        chatUserBean.save();
                        mRootView.loginSuccess(info);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                EMClient.getInstance().login(info.getUid(),"123456",new EMCallBack() {//回调
                                    @Override
                                    public void onSuccess() {
                                        EMClient.getInstance().groupManager().loadAllGroups();
                                        EMClient.getInstance().chatManager().loadAllConversations();
                                        LogUtil.d("mainEMClientChat:"+"登录聊天服务器成功！");

                                    }

                                    @Override
                                    public void onProgress(int progress, String status) {

                                    }

                                    @Override
                                    public void onError(int code, String message) {
                                        LogUtil.d("mainEMClientChat:"+"登录聊天服务器失败！");
                                    }
                                });
                            }
                        }).start();


                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                }));



    }

    @Override
    public void register(RegisterParam registerParam) {
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", registerParam.getMobile());
        map.put("code", registerParam.getCode());
        map.put("inviteCode", registerParam.getInviteCode());
        map.put("password", registerParam.getPassword());
        map.put("sex", registerParam.getSex());
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .REGISTER(map)
                .compose(RxUtils.handleResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<Object>(mRootView) {
                    @Override
                    public void onSuccess(Object o) {
                        mRootView.registerSuccess();
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                }));
    }
}
